package com.smt.modules.bus.service.impl;

import com.smt.common.enums.Code;
import com.smt.common.utils.*;
import com.smt.modules.bus.entity.MemberUserEntity;
import com.smt.modules.bus.entity.ProjectEntity;
import com.smt.modules.bus.entity.ScheduleEntity;
import com.smt.modules.bus.pojo.ProjectSiPojo;
import com.smt.modules.bus.pojo.UserNamePojo;
import com.smt.modules.bus.service.ProjectService;
import com.smt.modules.bus.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.smt.modules.bus.dao.ProjectTaskDao;
import com.smt.modules.bus.entity.ProjectTaskEntity;
import com.smt.modules.bus.service.ProjectTaskService;


@Service("projectTaskService")
public class ProjectTaskServiceImpl extends ServiceImpl<ProjectTaskDao, ProjectTaskEntity> implements ProjectTaskService {

    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private ProjectService projectService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<ProjectTaskEntity> page = this.selectPage(
                new Query<ProjectTaskEntity>(params).getPage(),
                new EntityWrapper<ProjectTaskEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProjectTaskEntity> getProjectTaskList(Long projectId) {
        List<ProjectTaskEntity> projectTaskEntities = baseMapper.selectProjectTask(projectId);
        return projectTaskEntities;
    }

    /**
     * 获取当前用户等级
     *
     * @param projectId
     * @param stageId
     * @return
     */
    @Override
    public Integer selectStageUserName(Long projectId, Integer stageId, MemberUserEntity memberUserEntity) {
        ProjectSiPojo projectSiPojo  = new ProjectSiPojo();
        projectSiPojo.setProjectId(projectId);
        projectSiPojo.setStage(stageId);
        List<UserNamePojo> userNamePojos = baseMapper.selectStageUserName(projectSiPojo);
        Long userId = memberUserEntity.getUserId();
        if(userNamePojos != null){
            for(UserNamePojo u : userNamePojos){
                if(u.getStageMemberId().equals(userId)){
                    //该用户是阶段负责人
                    return Constant.STAGE;
                }else if(u.getMemberId().equals(userId)){
                    //该用户是任务负责人
                    return Constant.TASK;
                }else if(u.getSubStageId().equals(userId)){
                    //该用户是子任务负责人
                    return Constant.SUB_TASK;
                }
            }
        }
        return Constant.FAIL_ID;
    }

    /**
     * 获取当前项目阶段下的各个任务
     *
     * @param projectId
     * @param stageId
     * @return
     */
    @Override
    public R getTaskList(Long projectId, Integer stageId){
        ProjectSiPojo projectSiPojo = new ProjectSiPojo();
        projectSiPojo.setProjectId(projectId);
        projectSiPojo.setStage(stageId);
        List<ProjectTaskEntity> projectTaskEntities = baseMapper.selectProjectTaskList(projectSiPojo);
        return R.ok().put("projectTaskList",projectTaskEntities);
    }

    @Override
    public List<ProjectTaskEntity> getAllTaskList(Long memberId) {
        return baseMapper.selectAllProjectTaskList(memberId);
    }

    /**
     * 删除任务
     *
     * @param ids
     * @param userId
     * @return
     */
    @Override
    public R delete(Long ids, Long userId) {
        ProjectTaskEntity projectTaskId = selectOne(new EntityWrapper<ProjectTaskEntity>()
                .eq("id", ids));
        Long projectId = null != projectTaskId?projectTaskId.getProjectId():new Long(0);
        synchronized (this) {
            boolean flag = false;
            ProjectEntity projectEntity = projectService.selectById(projectId);
            if (null != projectEntity) {
                ProjectTaskEntity projectTaskEntity = selectById(ids);
                if (null != projectTaskEntity) {
                    if (userId.equals(projectTaskEntity.getStageMemberId()) ||
                            userId.equals(projectEntity.getCreatedId())) {
                        flag = true;
                    }
                }
            }
            if(flag) {
                ProjectTaskEntity pro = new ProjectTaskEntity();
                pro.setId(ids);
                pro.setStatus(3);
                boolean b = updateById(pro);
                //删除日程记录
                if (b) {
                    ScheduleEntity scheduleEntity = new ScheduleEntity();
                    scheduleEntity.setPrior(-1);
                    scheduleService.update(scheduleEntity, new EntityWrapper<ScheduleEntity>()
                            .eq("task_id", ids));
                }
                return b ? R.ok("删除任务成功") :
                        R.error(EnumToKeyUtil.toInt(Code.DELETE_DATA_ERROR), "删除任务失败");
            }else{
                return  R.error(EnumToKeyUtil.toInt(Code.MISSING_PERMISSION), "您的权限不够");
            }
        }
    }
}
